import 'dart:io';

void main() {
  print('开始执行逻辑');
  //  // 1. 回调地狱
  // getFirstData().then((value1) {
  //    print("第一层结果:$value1");
  //    getSecondData().then((value2) {
  //     print("第二层结果:$value2");
  //     getThirdData().then((value3) {
  //       print("第三层结果:$value3");
  //     });
  //    });
  // });

  // 2. 采用真正的链式调用实现异步请求
  getFirstData().then((value) {
    print(value);
    return getSecondData();
  }).then((value) {
    print(value);
    return getThirdData();
  }).then((value) {
    print(value);
  });
}


// 前端 鸿蒙 的promise的链式调用

/***
 *  pending fullfiled rejected
 *  Future Uncompleted Completed with a value Completed with a error
 *  new Promise((resolve, reject) => {
 *     resolve(1)
 *   }).then(() => new Promise())
 *   .then(() => new Promise())
 *   .then(() => new Promise())
 *   .catch()
 *   .finally()
 * 
 * 
 * 
 * ****/


Future getFirstData(){
  return Future((){
    sleep(Duration(seconds: 2));
    print('获取第一层数据成功');
    return 'first data';
  });
}

Future getSecondData(){
  return Future(() {
    sleep(Duration(seconds: 2));
    print('获取第二层数据成功');
    return 'second data';
  });
}

Future getThirdData(){
  return Future(() {
    sleep(Duration(seconds: 2));
    print('获取第三层数据成功');
    return 'third data';
  });
}